package com.buddy.sds.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buddy.sds.auth.entity.SystemRole;
import com.buddy.sds.auth.entity.SystemUser;
import com.buddy.sds.auth.service.ISystemDepartmentService;
import com.buddy.sds.auth.service.ISystemRoleService;
import com.buddy.sds.auth.service.ISystemUserService;
import com.buddy.sds.auth.utils.PasswordUtil;
import com.buddy.sds.common.RestApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/auth/user")
public class SystemUserController {

    @Autowired
    private ISystemUserService systemUserService;

    @Autowired
    private ISystemDepartmentService departmentService;
    @Autowired
    private ISystemRoleService systemRoleService;

    @PutMapping("/updateUserRole")
    public RestApiResponse updateUserRole(@RequestParam("userId") String userId,@RequestBody List<String> roleIdList){

        systemUserService.updateUserRole(userId,roleIdList);

        return new RestApiResponse(0,"更新成功",null);
    }


    @GetMapping("/getUserRole")
    public RestApiResponse getUserRole(@RequestParam("userId") String userId){

        List<SystemRole> roles = systemRoleService.getRoleListByUser(userId,false);

        return new RestApiResponse(0,"",roles);
    }

    @GetMapping("/getUserList")
    public RestApiResponse getUserList(Page page){

       IPage<SystemUser> userIPage = systemUserService.page(page);

       return new RestApiResponse(0,"",userIPage);
    }

    @DeleteMapping("/deleteUser")
    public RestApiResponse deleteUser(@RequestParam("userId") String userId){
        //级联删除
        systemUserService.deleteCascade(userId);
        return new RestApiResponse(0,"删除成功",null);

    }

    @GetMapping("/getUser")
    public RestApiResponse getUser(@RequestParam("userId") String userId){
        SystemUser systemUser = systemUserService.getById(userId);

        if(systemUser != null){
            //去掉密码。。
            systemUser.setUserPassword(null);
            return new RestApiResponse(0,"",systemUser);
        }else{
            return new RestApiResponse(-1,"用户不存在",null);
        }
    }
    @PutMapping("/updateUser")
    public RestApiResponse updateUser(@RequestBody SystemUser systemUser,@RequestParam("updatePassword") boolean updatePassword){

        int status = -1;
        String message = null;

        do{

            if(StringUtils.isEmpty(systemUser.getUserId())){
                status = -1;
                message = "用户id为空";
                break;
            }


            SystemUser oldUser = systemUserService.getById(systemUser.getUserId());


            if(StringUtils.isEmpty(systemUser.getUserDepartment())){
                status = -1;
                message = "所属部门为空";
                break;
            }else{
                if(!departmentService.idExists(systemUser.getUserDepartment())){
                    status = -1;
                    message = "所属部门不存在";
                    break;
                }else{
                    oldUser.setUserDepartment(systemUser.getUserDepartment());
                }
            }


            if(oldUser == null){
                status = -1;
                message = "用户不存在";
                break;
            }

            if(StringUtils.isEmpty(systemUser.getUserNickname())){
                status = -1;
                message = "用户昵称为空";
                break;
            }else{
                oldUser.setUserNickname(systemUser.getUserNickname());
            }


            if(updatePassword){

                if(StringUtils.isEmpty(systemUser.getUserPassword())){
                    status = -1;
                    message = "用户密码为空";
                    break;
                }

                if(systemUser.getUserPassword().length() < 6 || systemUser.getUserPassword().length() > 20){
                    status = -1;
                    message = "密码长度应该在6-20个字符长度";
                    break;
                }

                oldUser.setUserPassword(PasswordUtil.encode( systemUser.getUserPassword()));
            }

            oldUser.setUserLock(systemUser.isUserLock());
            oldUser.setUserExpire(systemUser.getUserExpire());

            if(systemUserService.updateById(oldUser)){

                status = 0;
                message = "更新成功";

                log.info("用户:{} 更新信息成功 是否修改密码:{}",systemUser.getUserName(),updatePassword );

            }else{
                status = -1;
                message = "更新失败";

                log.error("用户:{} 更新信息失败 ",systemUser.getUserName() );
            }

        }while (false);

        return new RestApiResponse(status,message,null);
    }

    @PostMapping("/addUser")
    public RestApiResponse addUser(@RequestBody SystemUser systemUser){

        int status = -1;
        String message = null;

        do{
            if(StringUtils.isEmpty(systemUser.getUserNickname())){
                status = -1;
                message = "用户昵称为空";
                break;
            }

            if(StringUtils.isEmpty(systemUser.getUserDepartment())){
                status = -1;
                message = "所属部门为空";
                break;

            }else{
                if(!departmentService.idExists(systemUser.getUserDepartment())){
                    status = -1;
                    message = "所属部门不存在";
                    break;
                }
            }

            if(systemUserService.usernameExists(systemUser.getUserName())){
                status = -1;
                message = "账号已存在";
                break;
            }

            if(StringUtils.isEmpty(systemUser.getUserName())){
                status = -1;
                message = "用户账号为空";
                break;
            }

            if(StringUtils.isEmpty(systemUser.getUserPassword())){
                status = -1;
                message = "用户密码为空";
                break;
            }else{
                //加密密码
                systemUser.setUserPassword(PasswordUtil.encode(systemUser.getUserPassword()));
            }

            if(systemUser.getUserPassword().length() < 6 || systemUser.getUserPassword().length() > 20){
                status = -1;
                message = "密码长度应该在6-20个字符长度";
                break;
            }

            systemUser.setUserCreatetime(null);
            //默认锁定账户
            systemUser.setUserLock(true);

            if(systemUserService.save(systemUser)){

                status = 0;
                message = "创建成功";

                log.info("新增用户:",systemUser.getUserName());

            }else{
                status = -1;
                message = "创建失败,请检查是否存在相同的账号";

                log.error("新增用户失败:{}",systemUser.getUserName());
            }



        }while (false);

        return new RestApiResponse(status,message,null);
    }


}
